﻿namespace DateAccessQAWebpart
{
    using System;
    using System.Collections.Generic;

    using Microsoft.SharePoint;

    public class TargetFieldsDataCollectionBL
    {
        #region Fields

        List<DepartmentVisibleFields> _AvailableFields;

        #endregion Fields

        #region Properties

        public List<DepartmentVisibleFields> AvailableFields
        {
            get
            {
                if ( _AvailableFields == null )
                    _AvailableFields = new List<DepartmentVisibleFields>( );
                return this._AvailableFields;
            }
            set
            {
                this._AvailableFields = value;
            }
        }

        #endregion Properties

        #region Methods

        public void CleanDepartmentOrdering( string siteURL )
        {
            using ( SPSite sPSite = new SPSite( siteURL ) )
            {
                using ( SPWeb sPWeb = sPSite.OpenWeb( ) )
                {
                    SPList sPList = sPWeb.Lists[ "Department Administration List" ];
                    SPList sPList2 = sPWeb.Lists[ "Department Ordering Lookup" ];
                    SPQuery sPQuery = new SPQuery( );
                    foreach ( SPListItem sPListItem in sPList.Items )
                    {
                        string str = sPListItem[ "Department Name" ].ToString( );
                        sPQuery.Query = "<Where><Eq><FieldRef Name='Department_x0020_Owner' /><Value Type='Text'>" + str + "</Value></Eq></Where><OrderBy><FieldRef Name='Department_x0020_Owner' Ascending='True' /><FieldRef Name='Order_x0020_Number' Ascending='True' /></OrderBy>";
                        SPListItemCollection items = sPList2.GetItems( sPQuery );
                        int num = 1;
                        if ( items.Count > 0 )
                        {
                            foreach ( SPListItem sPListItem2 in items )
                            {
                                sPListItem2[ "Order Number" ] = num;
                                sPListItem2.Update( );
                                num++;
                            }
                        }
                    }
                }
            }
        }

        public void DeleteSelectedField( string siteURL, string sourceDepartment, string targetDepartment, string targetList, string fieldName )
        {
            using ( SPSite sPSite = new SPSite( siteURL ) )
            {
                DepartmentVisibleFields departmentVisibleFields = new DepartmentVisibleFields( );
                using ( SPWeb sPWeb = sPSite.OpenWeb( ) )
                {
                    SPList sPList = sPWeb.Lists[ "Department Access Administration" ];
                    SPQuery sPQuery = new SPQuery( );
                    sPQuery.Query = string.Concat( new string[ ]
                    {
                        "<Where><And><Eq><FieldRef Name='Department_x0020_Name' /><Value Type='Text'>",
                        sourceDepartment,
                        "</Value></Eq><And><Eq><FieldRef Name='Target_x0020_List_x0020_Name' /><Value Type='Text'>",
                        targetDepartment,
                        "</Value></Eq><Eq><FieldRef Name='Field_x0020_Name' /><Value Type='Text'>",
                        fieldName,
                        "</Value></Eq></And></And></Where>"
                    } );
                    SPListItemCollection items = sPList.GetItems( sPQuery );
                    if ( items.Count != 0 )
                    {
                        items[ 0 ].Delete( );
                        this.DeleteSelectedOrderField( siteURL, sourceDepartment, targetDepartment, fieldName );
                    }
                }
            }
        }

        public void DeleteSelectedOrderField( string siteURL, string sourceDepartment, string targetDepartment, string fieldName )
        {
            using ( SPSite sPSite = new SPSite( siteURL ) )
            {
                DepartmentVisibleFields departmentVisibleFields = new DepartmentVisibleFields( );
                using ( SPWeb sPWeb = sPSite.OpenWeb( ) )
                {
                    SPList sPList = sPWeb.Lists[ "Department Ordering Lookup" ];
                    SPQuery sPQuery = new SPQuery( );
                    sPQuery.Query = string.Concat( new string[ ]
                    {
                        "<Where><And><Eq><FieldRef Name='Field_x0020_Name' /><Value Type='Text'>",
                        fieldName,
                        "</Value></Eq><And><Eq><FieldRef Name='Source_x0020_Department' /><Value Type='Text'>",
                        targetDepartment,
                        "</Value></Eq><Eq><FieldRef Name='Department_x0020_Owner' /><Value Type='Text'>",
                        sourceDepartment,
                        "</Value></Eq></And></And></Where>"
                    } );
                    SPListItemCollection items = sPList.GetItems( sPQuery );
                    if ( items.Count != 0 )
                        items[ 0 ].Delete( );
                }
            }
        }

        public bool DoesFieldExistingOrderList( string siteURL, string sourceDepartment, string fieldName, string targetDepartment )
        {
            using ( SPSite sPSite = new SPSite( siteURL ) )
            {
                using ( SPWeb sPWeb = sPSite.OpenWeb( ) )
                {
                    SPList sPList = sPWeb.Lists[ "Department Ordering Lookup" ];
                    SPQuery sPQuery = new SPQuery( );
                    sPQuery.Query = string.Concat( new string[ ]
                    {
                        "<Where><And><Eq><FieldRef Name='Field_x0020_Name' /><Value Type='Text'>",
                        fieldName,
                        "</Value></Eq><And><Eq><FieldRef Name='Source_x0020_Department' /><Value Type='Text'>",
                        sourceDepartment,
                        "</Value></Eq><Eq><FieldRef Name='Department_x0020_Owner' /><Value Type='Text'>",
                        targetDepartment,
                        "</Value></Eq></And></And></Where>"
                    } );
                    SPListItemCollection items = sPList.GetItems( sPQuery );
                    if ( items.Count == 0 )
                        return false;
                    else
                        return true;
                }
            }
        }

        public void GetAllDepartmentSelectedFields( string siteURL, string sourceDepartment )
        {
            using ( SPSite sPSite = new SPSite( siteURL ) )
            {
                DepartmentVisibleFields departmentVisibleFields = new DepartmentVisibleFields( );
                using ( SPWeb sPWeb = sPSite.OpenWeb( ) )
                {
                    SPList sPList = sPWeb.Lists[ "Department Access Administration" ];
                    SPQuery sPQuery = new SPQuery( );
                    sPQuery.Query = "<Where><Eq><FieldRef Name='Department_x0020_Name' /><Value Type='Text'>" + sourceDepartment.Trim( ) + "</Value></Eq></Where><OrderBy><FieldRef Name='Target_x0020_List_x0020_Name' Ascending='True' /><FieldRef Name='Field_x0020_Name' Ascending='True' /></OrderBy>";
                    SPListItemCollection items = sPList.GetItems( sPQuery );
                    foreach ( SPItem sPItem in items )
                    {
                        departmentVisibleFields = new DepartmentVisibleFields( );
                        departmentVisibleFields.SourceDepartmentName = sourceDepartment;
                        departmentVisibleFields.TargetDepartmentName = sPItem[ "Target Department Name" ].ToString( ).Trim( );
                        departmentVisibleFields.TargetFieldName = sPItem[ "Field Name" ].ToString( ).Trim( );
                        departmentVisibleFields.TargetListName = sPItem[ "Target List Name" ].ToString( ).Trim( );
                        departmentVisibleFields.ReadOnly = false;
                        AvailableFields.Add( departmentVisibleFields );
                    }
                }
            }
        }

        public int GetMaxOrderingNumber( string siteURL, string ownerDepartment )
        {
            int num = 1;
            int result;
            try
            {
                SPSite sPSite = new SPSite( siteURL );
                using ( SPWeb sPWeb = sPSite.OpenWeb( ) )
                {
                    SPList sPList = sPWeb.Lists[ "Department Ordering Lookup" ];
                    SPQuery sPQuery = new SPQuery( );
                    sPQuery.Query = "<Where><Eq><FieldRef Name='Department_x0020_Owner' /><Value Type='Text'>" + ownerDepartment + "</Value></Eq></Where><OrderBy><FieldRef Name='Order_x0020_Number' Ascending='False' /></OrderBy>";
                    SPListItemCollection items = sPList.GetItems( sPQuery );
                    if ( items.Count == 0 )
                    {
                        num = 1;
                    }
                    else
                    {
                        num = int.Parse( items[ 0 ][ "Order Number" ].ToString( ) );
                        num++;
                    }
                }
                result = num;
            }
            catch ( Exception )
            {
                result = num;
            }
            return result;
        }

        public void GetSourceTargetDepartmentSelectedFields( string siteURL, string sourceDepartment, string targetDepartment )
        {
            using ( SPSite sPSite = new SPSite( siteURL ) )
            {
                DepartmentVisibleFields departmentVisibleFields = new DepartmentVisibleFields( );
                using ( SPWeb sPWeb = sPSite.OpenWeb( ) )
                {
                    SPList sPList = sPWeb.Lists[ "Department Access Administration" ];
                    SPQuery sPQuery = new SPQuery( );
                    sPQuery.Query = string.Concat( new string[ ]
                    {
                        "<Where><And><Eq><FieldRef Name='Department_x0020_Name' /><Value Type='Text'>",
                        sourceDepartment,
                        "</Value></Eq><Eq><FieldRef Name='Target_x0020_List_x0020_Name' /><Value Type='Text'>",
                        targetDepartment,
                        "</Value></Eq></And></Where><OrderBy><FieldRef Name='Target_x0020_List_x0020_Name' Ascending='True' /><FieldRef Name='Field_x0020_Name' Ascending='True' /></OrderBy>"
                    } );
                    SPListItemCollection items = sPList.GetItems( sPQuery );
                    foreach ( SPItem sPItem in items )
                    {
                        departmentVisibleFields = new DepartmentVisibleFields( );
                        departmentVisibleFields.SourceDepartmentName = sourceDepartment;
                        departmentVisibleFields.TargetDepartmentName = sPItem.Fields[ "Target Department Name" ].ToString( ).Trim( );
                        departmentVisibleFields.TargetFieldName = sPItem.Fields[ "Field Name" ].ToString( ).Trim( );
                        departmentVisibleFields.TargetListName = sPItem.Fields[ "Target List Name" ].ToString( ).Trim( );
                        departmentVisibleFields.ReadOnly = false;
                        AvailableFields.Add( departmentVisibleFields );
                    }
                }
            }
        }

        public bool IsFieldSelected( string siteURL, string sourceDepartment, string targetDepartment, string fieldName )
        {
            using ( SPSite sPSite = new SPSite( siteURL ) )
            {
                using ( SPWeb sPWeb = sPSite.OpenWeb( ) )
                {
                    SPList sPList = sPWeb.Lists[ "Department Access Administration" ];
                    SPQuery sPQuery = new SPQuery( );
                    sPQuery.Query = string.Concat( new string[ ]
                    {
                        "<Where><And><Eq><FieldRef Name='Department_x0020_Name'/><Value Type='Text'>",
                        sourceDepartment,
                        "</Value></Eq><And><Eq><FieldRef Name='Target_x0020_List_x0020_Name' /><Value Type='Text'>",
                        targetDepartment,
                        "</Value></Eq><Eq><FieldRef Name='Field_x0020_Name' /><Value Type='Text'>",
                        fieldName,
                        "</Value></Eq></And></And></Where>"
                    } );
                    SPListItemCollection items = sPList.GetItems( sPQuery );
                    if ( items.Count > 0 )
                        return true;
                    else
                        return false;
                }
            }
        }

        public void SaveSelectedField( string siteURL, string sourceDepartment, string targetDepartment, string targetList, string fieldName )
        {
            this.SaveSelectedField( siteURL, sourceDepartment, targetDepartment, targetList, fieldName, false );
        }

        public void SaveSelectedField( string siteURL, string sourceDepartment, string targetDepartment, string targetList, string fieldName, bool readOnly )
        {
            using ( SPSite sPSite = new SPSite( siteURL ) )
            {
                DepartmentVisibleFields departmentVisibleFields = new DepartmentVisibleFields( );
                using ( SPWeb sPWeb = sPSite.OpenWeb( ) )
                {
                    SPList sPList = sPWeb.Lists[ "Department Access Administration" ];
                    SPListItem sPListItem = sPList.Items.Add( );
                    sPListItem[ "Title" ] = sourceDepartment;
                    sPListItem[ "Department Name" ] = sourceDepartment;
                    sPListItem[ "Target List Name" ] = targetList;
                    sPListItem[ "Field Name" ] = fieldName;
                    sPListItem[ "Target Department Name" ] = targetDepartment;
                    if ( this.ValidateItemBeforeInsertion( siteURL, sourceDepartment, targetDepartment, targetList, fieldName, false ) )
                    {
                        sPListItem.Update( );
                        SPList sPList2 = sPWeb.Lists[ "Department Ordering Lookup" ];
                        if ( !this.DoesFieldExistingOrderList( siteURL, sourceDepartment, fieldName, targetDepartment ) )
                        {
                            SPListItem sPListItem2 = sPList2.Items.Add( );
                            sPListItem2[ "Title" ] = fieldName;
                            sPListItem2[ "Source Information List" ] = targetDepartment;
                            int maxOrderingNumber = this.GetMaxOrderingNumber( siteURL, sourceDepartment );
                            sPListItem2[ "Order Number" ] = maxOrderingNumber;
                            sPListItem2[ "Field Name" ] = fieldName;
                            sPListItem2[ "Source Department" ] = targetDepartment;
                            sPListItem2[ "Department Owner" ] = sourceDepartment;
                            sPListItem2.Update( );
                            sPList2.Update( );
                        }
                    }
                    else
                        sPListItem.Delete( );
                    sPList.Update( );
                }
            }
        }

        private bool ValidateItemBeforeInsertion( string siteURL, string sourceDepartment, string targetDepartment, string targetList, string fieldName, bool readOnly )
        {
            using ( SPSite sPSite = new SPSite( siteURL ) )
            {
                DepartmentVisibleFields departmentVisibleFields = new DepartmentVisibleFields( );
                using ( SPWeb sPWeb = sPSite.OpenWeb( ) )
                {
                    SPList sPList = sPWeb.Lists[ "Department Access Administration" ];
                    SPQuery sPQuery = new SPQuery( );
                    sPQuery.Query = string.Concat( new string[ ]
                    {
                        "<Where><And><Eq><FieldRef Name='Department_x0020_Name' /><Value Type='Text'>",
                        sourceDepartment,
                        "</Value></Eq><And><Eq><FieldRef Name='Target_x0020_List_x0020_Name' /><Value Type='Text'>",
                        targetDepartment,
                        "</Value></Eq><Eq><FieldRef Name='Field_x0020_Name' /><Value Type='Text'>",
                        fieldName,
                        "</Value></Eq></And></And></Where>"
                    } );
                    SPListItemCollection items = sPList.GetItems( sPQuery );
                    if ( items.Count == 0 )
                        return true;
                    else
                        return false;
                }
            }
        }

        #endregion Methods
    }
}